﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Condimar.Entities;

namespace Condimar.Repository
{
    public class ProveedorDAO : MasterRepository, IProveedorDAO
    {
        public List<Proveedor> GetAllFromProveedor()
        {
            var query = from c in Context.Proveedors
                        select c;
            return query.ToList();
        }

        public List<Proveedor> GetFromProveedorByRazonSocial(string RazonSocial)
        {
            if (RazonSocial != "")
            {
                var query = from c in Context.Proveedors
                            where c.RazonSocial.ToUpper().Contains(RazonSocial.ToUpper())
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.Proveedors
                             select c;
                return query1.ToList();
            }
        }


        public List<Proveedor> GetFromProveedorByRuc(string ruc)
        {
            if (ruc != "")
            {
                var query = from c in Context.Proveedors
                            where c.Ruc.ToUpper().Contains(ruc.ToUpper())
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.Proveedors
                             select c;
                return query1.ToList();
            }
        }

        public Proveedor GetFromProveedorById(int id)
        {
            return Context.Proveedors.Find(id);
        }

        public void AddProveedor(Proveedor proveedor)
        {
            Context.Proveedors.Add(proveedor);
            Context.SaveChanges();
        }

        public void UpdateProveedor(Proveedor proveedor)
        {
            Context.Entry(proveedor).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveProveedor(int id)
        {
            var existe = from c in Context.Proveedors
                         where c.Id.Equals(id)
                         select c;

            Context.Proveedors.Remove(existe.SingleOrDefault());
            Context.SaveChanges();
        }
    }
}

       